# -*- coding = utf-8 -*-
# @Time : 2023/3/3 16:28
# @Author : 龙王赘婿_彪少
# @File : binascii_demo.py
# @Software: PyCharm

# 功能：

import binascii


class BINA:
    __doc__ = []
    __doc__ += ["binascii"]
    __doc__ += ["https://blog.csdn.net/m0_37816922/article/details/127354410",
                "https://feishujun.blog.csdn.net/article/details/114704320?spm=1001.2101.3001.6650.4&depth_1-utm_relevant_index=9"]
    __doc__ += ["binascii顾名思义用于二进制和ASCII之间的转换",
                "主要提供了hex, uu, base64, qp, hqx五种编解码方式。",
                "其函数名称为a2b_xx或者b2a_xx，xx替换成不同的编码方式。"]
    __doc__ += ["hexlify, unhexlify, b2a_hex, a2b_hex"]

    def doc(self):
        for it in self.__doc__:
            print(it)

    def fun_hex(self):
        # hex 函数 返回一个string
        print(f"num={15},numhex={hex(15)}")

        # ASCII 码
        n1 = b'liuzhibiao13077322793'
        # n1 = b'1'
        print(f"n1={n1},type={type(n1)}")

        # byte->hex
        n2 = binascii.b2a_hex(n1)
        print(f"n2={n2},type={type(n2)}")

        # n3 = binascii.b2a_hex(n2)   # 无限循环 捂脸笑
        # print(f"n3={n3},type={type(n3)}")

        n4 = binascii.a2b_hex(n2)
        print(f"n4={n4},type={type(n4)}")

    def fun_uu(self):
        # unix to unit
        n1 = b'\x01\x10\x11'
        print(f"n1={n1},type={type(n1)}")
        # byte->uu
        n2 = binascii.b2a_uu(n1)
        print(f"n2={n2},type={type(n2)}")

        n3 = binascii.a2b_uu(n2)
        print(f"n3={n3},type={type(n3)}")

    def fun_base64(self):
        n1 = b'\x01\x10\x11'
        print(f"n1={n1},type={type(n1)}")
        # byte->base64
        n2 = binascii.b2a_base64(n1)
        print(f"n2={n2},type={type(n2)}")

        n3 = binascii.a2b_base64(n2)
        print(f"n3={n3},type={type(n3)}")

    def fun_qp(self):
        n1 = b'\x01\x10\x11'
        print(f"n1={n1},type={type(n1)}")
        # byte->qp
        n2 = binascii.b2a_qp(n1)
        print(f"n2={n2},type={type(n2)}")

        n3 = binascii.a2b_qp(n2)
        print(f"n3={n3},type={type(n3)}")

    def fun_in(self):
        # 内置函数
        # chr(): 把一个整形转换成ASCII码表中对应的单个字符
        # ord(): 把ASCII码表中的字符转换成对应的整形
        # hex(): 把十进制转换成16进制字符
        # oct(): 把十进制转换成八进制字符
        # bin(): 把十进制整形转换成二进制字符
        num = 31
        print(num)
        print(chr(num))
        print(ord(chr(num)))
        print(hex(num))
        print(oct(num))
        print(bin(num))


    def __init__(self):
        # self.doc()
        # self.fun_hex()
        # self.fun_uu()
        # self.fun_base64()
        # self.fun_qp()
        self.fun_in()


if __name__ == "__main__":
    b = BINA()
